//
//  OSRecommentViewController.m
//  百思不得姐
//
//  Created by gl on 16/2/22.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSRecommentViewController.h"
#import "OSRecommentCategoriesTableViewCell.h"
#import "OSRecommendCategory.h"
#import "OSRecommendUser.h"
#import "OSRecommendUserCell.h"

#define OSSelectedCategory self.categories[self.categoryTableView.indexPathForSelectedRow.row]


@interface OSRecommentViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,copy)NSMutableArray *categories;
@property (nonatomic,copy)NSArray *users;
@property (strong, nonatomic) IBOutlet UITableView *categoryTableView;
@property (weak, nonatomic) IBOutlet UITableView *usersTableView;

@property (nonatomic,strong)NSMutableDictionary *params;

@property (nonatomic,strong)AFHTTPSessionManager *manager;


@end




@implementation OSRecommentViewController


static NSString *const categoryID = @"categoryCell";

static NSString *const userID = @"userCell";


-(AFHTTPSessionManager *)manager{
    if (_manager) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self setupTableView];
    
    [self setupRefresh];
    
    [self loadCategory];
}
-(void)setupRefresh{
    self.usersTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewUsers)];
    self.usersTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreUuers)];
    self.usersTableView.footer.hidden = YES;
}
-(void)loadMoreUuers{

}
-(void)loadNewUsers{
    
    OSRecommendCategory *rc = OSSelectedCategory;
    
    rc.currentPage = 1;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"subscribe";
    params[@"category_id"] = @(rc.id);
    params[@"page"] = @(rc.currentPage);
    self.params = params;
    [self.manager GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask *  task, id   responseObject) {
        NSArray *users = [OSRecommendUser objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        [rc.users removeAllObjects];
        [rc.users addObjectsFromArray:users];
        
        if (self.params != params) return ;
        
        [self.usersTableView.header endRefreshing];
        
        [self.usersTableView reloadData];
        
        rc.total = [responseObject[@"total"] integerValue];
        
        [self checkFooterState];
        
    } failure:^(NSURLSessionDataTask *  task, NSError *  error) {
        
        if (self.params != params) return ;
        
        [self.usersTableView.header endRefreshing];
    }];

}
-(void)checkFooterState{
    OSRecommendCategory *rc = OSSelectedCategory;
    
    self.usersTableView.footer.hidden = (rc.users.count == 0);
    
    if (rc.users.count == rc.total) {
        [self.usersTableView.footer noticeNoMoreData];
    }else{
        [self.usersTableView.footer endRefreshing];
    }
}
-(void)loadCategory{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"category";
    params[@"c"] = @"subscribe";
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
    

    
    [self.manager GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask * task, id   responseObject) {
        
        [SVProgressHUD dismiss];
        
        self.categories = [OSRecommendCategory mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        [self.categoryTableView reloadData];
        
        [self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        
    } failure:^(NSURLSessionDataTask *  task, NSError *  error) {
        
        NSLog(@"%@",error.userInfo);
        
        
        [SVProgressHUD showErrorWithStatus:@"加载推荐信息失败"];
    }];
    

}
-(void)setupTableView{
    
    
    self.title = @"推荐关注";
    
    
    self.view.backgroundColor = OSRGBColor(244, 244, 244);
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    
    self.usersTableView.contentInset = self.categoryTableView.contentInset;
    self.usersTableView.rowHeight = 60;
    
    
    [self.categoryTableView registerNib:[UINib nibWithNibName:@"OSRecommentCategoriesTableViewCell" bundle:nil] forCellReuseIdentifier:categoryID];
    [self.usersTableView registerNib:[UINib nibWithNibName:@"OSRecommendUserCell" bundle:nil] forCellReuseIdentifier:userID];
    
}

#pragma mark - UITableVIewDataSource


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (tableView == self.categoryTableView) return self.categories.count;

    [self checkFooterState];
    
    return [OSSelectedCategory users].count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.categoryTableView) {
        
        OSRecommentCategoriesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryID];
        
        cell.Category = self.categories[indexPath.row];
        
        
        return cell;
    }else{
        
        OSRecommendUserCell *userCell = [tableView dequeueReusableCellWithIdentifier:userID];
        
        OSRecommendCategory *c = self.categories[self.categoryTableView.indexPathForSelectedRow.row];
        userCell.user = c.users[indexPath.row];
        return userCell;
    }
}
#pragma mark - UITableViewDalegate


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OSRecommendCategory *c = self.categories[indexPath.row];
    
    [self.usersTableView reloadData];

    if (!c.users.count) {
        [self.usersTableView.header beginRefreshing];
        
    }
}
-(void)dealloc{
    
    [SVProgressHUD dismiss];
    
    [self.manager.operationQueue cancelAllOperations];
}
@end
